using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CreatingRainbows
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MagentaRoom : GameScene
    {
        Door grayDoor;
        Texture2D grayDoorImg;

        Door puzzleDoor;
        public Door PuzzleDoor
        {
            get { return puzzleDoor; }
            set { puzzleDoor = value; }
        }

        Door puzzleDoor1;
        public Door PuzzleDoor1
        {
            get { return puzzleDoor1; }
            set { puzzleDoor1 = value; }
        }

        Texture2D puzzleDoorImg;

        SpriteBatch spriteBatch;

        int enterance;
        public int Enterance
        {
            get { return enterance; }
            set { enterance = value; }
        }

        bool goal0;
        public bool Goal0
        {
            get { return goal0; }
            set { goal0 = value; }
        }
        bool goal1;
        public bool Goal1
        {
            get { return goal1; }
            set { goal1 = value; }
        }

        public MagentaRoom(Game1 game, Texture2D textureBack, Texture2D textureFront)
            : base(game)
        {
            mainReference = game;

            Components.Add(new ImageComponent(game, textureBack, ImageComponent.DrawMode.Stretch));
            Components.Add(new ImageComponent(game, textureFront, ImageComponent.DrawMode.Center));

            Initialize();

            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            grayDoor = new Door(mainReference);
            this.Components.Add(grayDoor);
            grayDoor.Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            puzzleDoor = new Door(mainReference);
            this.Components.Add(puzzleDoor);
            puzzleDoor.Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            puzzleDoor1 = new Door(mainReference);
            this.Components.Add(puzzleDoor1);
            puzzleDoor1 .Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            goal0 = false;
            goal1 = false;

            base.Initialize();

            LoadContent();


        }

        protected override void LoadContent()
        {
            grayDoorImg = mainReference.Content.Load<Texture2D>("grayDoor");
            grayDoor.LoadContent(spriteBatch, ref grayDoorImg);

            puzzleDoorImg = mainReference.Content.Load<Texture2D>("puzzleDoor");
            puzzleDoor.LoadContent(spriteBatch, ref puzzleDoorImg);
            puzzleDoor1.LoadContent(spriteBatch, ref puzzleDoorImg);

            enteranceLocs = new List<Vector2>();
            enteranceLocs.Add(new Vector2(grayDoor.position.X + grayDoor.source.Width + 5, (grayDoor.source.Height / 2) - main.source.Height / 2));
            enteranceLocs.Add(new Vector2(mainReference.SceneWidth - puzzleDoor.source.Width - main.source.Width - 5, mainReference.SceneHeight-(puzzleDoor.source.Height / 2) - main.source.Height / 2));
            enteranceLocs.Add(new Vector2(mainReference.SceneWidth - puzzleDoor1.source.Width - main.source.Width - 5,(puzzleDoor1.source.Height / 2) - main.source.Height / 2));

            base.LoadContent();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            DoorCollision();

            base.Update(gameTime);
        }

        public override void CheckGoals()
        {
            if (mainReference.MagentaRoomScene.Goal0 && mainReference.MagentaRoomScene.Goal1)
            {
                mainReference.MainRoomScene.Lvl1UltGoal = true;
            }
            base.CheckGoals();
        }

        private void DoorCollision()
        {
            if (main.Colliding(grayDoor))
            {
                this.Hide();
                mainReference.MainRoomScene.Enterance = 1;
                mainReference.MainRoomScene.Show();
                

                mainReference.ActiveScene = mainReference.MainRoomScene;
            }

            if (main.Colliding(puzzleDoor) && puzzleDoor.IsLocked == true)
            {
                this.Hide();
                mainReference.ISpyRoomScene.Show();

                mainReference.ActiveScene = mainReference.ISpyRoomScene;
            }

            if (main.Colliding(puzzleDoor1) && puzzleDoor1.IsLocked == true)
            {
                this.Hide();
                mainReference.ISpyRoomScene1.Show();

                mainReference.ActiveScene = mainReference.ISpyRoomScene1;
            }
        }

        public override void Show()
        {
            grayDoor.position = Localize(0, 0);

            puzzleDoor.position = Localize((mainReference.SceneWidth) - (puzzleDoor.source.Width), (mainReference.SceneHeight) - (puzzleDoor.source.Height));

            puzzleDoor1.position = Localize((mainReference.SceneWidth)-(puzzleDoor1.source.Width), 0);

            #region unused placement code
            //main.position = Localize((mainReference.SceneWidth / 2) - (main.source.Width / 2), (mainReference.SceneHeight / 2) - (main.source.Width / 2));
            //main.position.X = mainReference.ScreenWidth / 2 - main.source.Width / 2;
            //main.position.Y = mainReference.ScreenHeight / 2 - main.source.Height / 2;
            //grayDoor.position.Y = (mainReference.ScreenHeight / 2) - (mainReference.SceneHeight / 2);
            //grayDoor.position.X = (mainReference.ScreenWidth / 2) - (mainReference.SceneWidth / 2);
            //puzzleDoor.position.Y = (((mainReference.ScreenHeight) - ((mainReference.ScreenHeight / 2) - (mainReference.SceneHeight / 2))) - puzzleDoor.source.Height);
            //puzzleDoor.position.X = (((mainReference.ScreenWidth) - ((mainReference.ScreenWidth / 2) - (mainReference.SceneWidth / 2))) - puzzleDoor.source.Width);
            #endregion

            Enter(enterance);

            base.Show();
        }

        public void Enter(int enterance1)
        {
            switch (enterance1)
            {
                //coming from the monochrome room
                case 1:
                    main.position = Localize(enteranceLocs[0].X, enteranceLocs[0].Y);
                    break;
                //coming from the puzzle room
                case 2:
                    main.position = Localize(enteranceLocs[1].X, enteranceLocs[1].Y);
                    break;
                case 3:
                    main.position = Localize(enteranceLocs[2].X, enteranceLocs[2].Y);
                    break;
                default:
                    main.position = Localize((mainReference.SceneWidth / 2) - (main.source.Width / 2), (mainReference.SceneHeight / 2) - (main.source.Width / 2));
                    break;
            }
        }
    }
}
